package it.blueocean.acanto.taglibrary.jsp.taglib.tags.datatable;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.AbstractComponentStateBean;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.commands.SetComponentsPropertiesTag;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.dataset.DataSet;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.dataset.OrderByClause;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.utils.HtmlPrintHelper;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.utils.HttpUtils;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataTableColHeaderTag extends SetComponentsPropertiesTag
{
	private static final long serialVersionUID = 5723105121296894954L;

	private final static Logger logger_ = LoggerFactory.getLogger(DataTableColHeaderTag.class);

	private OrderByClause orderBy_;

	private boolean omitThTag_;

	private String htmlClass_;

	private String htmlStyle_;

	private String ascImg_;

	private String descImg_;

	private String align_;

	private String valign_;

	private String rowSpan_;

	private String colSpan_;

	private String width_;

	private String height_;

	private String value_;

	private String orderByExpr_ = null;

	private String omitThTagExpr_ = null;

	private String htmlClassExpr_;

	private String htmlStyleExpr_;

	private String ascImgExpr_;

	private String descImgExpr_;

	private String alignExpr_;

	private String valignExpr_;

	private String rowSpanExpr_;

	private String colSpanExpr_;

	private String widthExpr_;

	private String heightExpr_;

	private String valueExpr_;

	private DataSet dataset_;

	private String datasetName_;

	private boolean hasContent_;

	protected void evaluateExpressions() throws JspException
	{
		super.evaluateExpressions();
		DataTableTag localDataTableTag = (DataTableTag) findAncestorWithClass(this, DataTableTag.class);
		if(localDataTableTag != null)
		{
			this.dataset_ = localDataTableTag.getDataset();
			this.datasetName_ = localDataTableTag.getDataset().getId();
		}
		else
		{
			throw new JspException(
					"Cannot find an appropriate dataset, tag DataTableColHeaderTag must be inside a DataTableTag tag");
		}
		if(this.orderByExpr_ != null)
		{
			String str = (String) ExpressionEvaluatorManager.evaluate("orderBy", this.orderByExpr_, String.class, this,
					this.pageContext);
			this.orderBy_ = new OrderByClause(str);
		}
		else
		{
			this.orderBy_ = null;
		}
		if(this.omitThTagExpr_ != null)
			this.omitThTag_ = ((Boolean) ExpressionEvaluatorManager.evaluate("omitThTag", this.omitThTagExpr_,
					Boolean.class, this, this.pageContext)).booleanValue();
		else
			this.omitThTag_ = false;
		if(this.htmlClassExpr_ != null)
			this.htmlClass_ = ((String) ExpressionEvaluatorManager.evaluate("htmlClass", this.htmlClassExpr_,
					String.class, this, this.pageContext));
		else
			this.htmlClass_ = null;
		if(this.htmlStyleExpr_ != null)
			this.htmlStyle_ = ((String) ExpressionEvaluatorManager.evaluate("style", this.htmlStyleExpr_, String.class,
					this, this.pageContext));
		else
			this.htmlStyle_ = null;
		if(this.ascImgExpr_ != null)
			this.ascImg_ = ((String) ExpressionEvaluatorManager.evaluate("ascImg", this.ascImgExpr_, String.class,
					this, this.pageContext));
		else
			this.ascImg_ = null;
		if((localDataTableTag != null) && (localDataTableTag.getDefaultAscImg() != null)
				&& (!(localDataTableTag.getDefaultAscImg().equals(""))))
			this.ascImg_ = localDataTableTag.getDefaultAscImg();
		if(this.descImgExpr_ != null)
			this.descImg_ = ((String) ExpressionEvaluatorManager.evaluate("descImg", this.descImgExpr_, String.class,
					this, this.pageContext));
		else
			this.descImg_ = null;
		if((localDataTableTag != null) && (localDataTableTag.getDefaultDescImg() != null)
				&& (!(localDataTableTag.getDefaultDescImg().equals(""))))
			this.descImg_ = localDataTableTag.getDefaultDescImg();
		if(this.alignExpr_ != null)
			this.align_ = ((String) ExpressionEvaluatorManager.evaluate("align", this.alignExpr_, String.class, this,
					this.pageContext));
		else
			this.align_ = null;
		if(this.valignExpr_ != null)
			this.valign_ = ((String) ExpressionEvaluatorManager.evaluate("valign", this.valignExpr_, String.class,
					this, this.pageContext));
		else
			this.valign_ = null;
		if(this.rowSpanExpr_ != null)
			this.rowSpan_ = ((String) ExpressionEvaluatorManager.evaluate("rowSpan", this.rowSpanExpr_, String.class,
					this, this.pageContext));
		else
			this.rowSpan_ = null;
		if(this.colSpanExpr_ != null)
			this.colSpan_ = ((String) ExpressionEvaluatorManager.evaluate("colSpan", this.colSpanExpr_, String.class,
					this, this.pageContext));
		else
			this.colSpan_ = null;
		if(this.heightExpr_ != null)
			this.height_ = ((String) ExpressionEvaluatorManager.evaluate("height", this.heightExpr_, String.class,
					this, this.pageContext));
		else
			this.height_ = null;
		if(this.widthExpr_ != null)
			this.width_ = ((String) ExpressionEvaluatorManager.evaluate("width", this.widthExpr_, String.class, this,
					this.pageContext));
		else
			this.width_ = null;
		if(this.valueExpr_ != null)
			this.value_ = ((String) ExpressionEvaluatorManager.evaluate("value", this.valueExpr_, String.class, this,
					this.pageContext));
		else
			this.value_ = null;
	}

	public int doStartTag(AbstractComponentStateBean paramAbstractComponentStateBean) throws JspException
	{
		evaluateExpressions();
		if(this.orderBy_ != null)
			setProperty(this.datasetName_ + ".orderBy", this.orderBy_.toString());
		JspWriter localJspWriter = this.pageContext.getOut();
		try
		{
			if(!(this.omitThTag_))
			{
				HtmlPrintHelper.printOpenedHtmlStartTag("th", this.htmlClass_, this.htmlStyle_, localJspWriter);
				HtmlPrintHelper.printAttribute("align", this.align_, localJspWriter);
				HtmlPrintHelper.printAttribute("valign", this.valign_, localJspWriter);
				HtmlPrintHelper.printAttribute("rowSpan", this.rowSpan_, localJspWriter);
				HtmlPrintHelper.printAttribute("colSpan", this.colSpan_, localJspWriter);
				HtmlPrintHelper.printAttribute("width", this.width_, localJspWriter);
				HtmlPrintHelper.printAttribute("height", this.height_, localJspWriter);
				if(isAscOrdered())
					HtmlPrintHelper.printAttribute("class", "asc_ordered", localJspWriter);
				else if(isDescOrdered())
					HtmlPrintHelper.printAttribute("class", "desc_ordered", localJspWriter);
				HtmlPrintHelper.printClosedHtmlStartTag(localJspWriter);
			}
			if(!(getProperties().isEmpty()))
			{
				HtmlPrintHelper.printOpenedHtmlStartTag("a", this.htmlClass_, this.htmlStyle_, localJspWriter);
				HtmlPrintHelper.printAttribute(
						"href",
						HttpUtils.generateSetPropertyURL(this.properties_,
								(HttpServletRequest) this.pageContext.getRequest()), localJspWriter);
				HtmlPrintHelper.printClosedHtmlStartTag(localJspWriter);
			}
			if(this.value_ != null)
			{
				localJspWriter.print(this.value_);
				if(this.value_.length() > 0)
					this.hasContent_ = true;
				return 0;
			}
			return 2;
		}
		catch (IOException localIOException)
		{
			logger_.error(localIOException.getMessage());
		}
		return 6;
	}

	public int doAfterBody(AbstractComponentStateBean paramAbstractComponentStateBean) throws JspException
	{
		JspWriter localJspWriter = getPreviousOut();
		try
		{
			this.hasContent_ = false;
			if((this.bodyContent != null) && (this.bodyContent.getString().length() > 0))
			{
				localJspWriter.print(this.bodyContent.getString());
				this.hasContent_ = true;
			}
		}
		catch (IOException localIOException)
		{
			logger_.error(localIOException.getMessage());
		}
		return 6;
	}

	public int doEndTag(AbstractComponentStateBean paramAbstractComponentStateBean) throws JspException
	{
		JspWriter localJspWriter = this.pageContext.getOut();
		try
		{
			if(this.orderBy_ != null)
				if((isAscOrdered()) && (this.ascImg_ != null))
				{
					if(this.hasContent_)
						localJspWriter.print(' ');
					localJspWriter.print(" <img src=\"" + this.ascImg_
							+ "\" alt=\"asc order\"   style=\"border:0px;\" />");
				}
				else if((isDescOrdered()) && (this.descImg_ != null))
				{
					if(this.hasContent_)
						localJspWriter.print(' ');
					localJspWriter.print(" <img src=\"" + this.descImg_
							+ "\" alt=\"desc order\"   style=\"border:0px;\" />");
				}
			if(!(getProperties().isEmpty()))
				localJspWriter.print("</a>");
			if(!(this.omitThTag_))
				localJspWriter.print("</th>");
		}
		catch (IOException localIOException)
		{
			logger_.error(localIOException.getMessage());
		}
		return 6;
	}

	public boolean isDescOrdered()
	{
		return ((this.orderBy_ != null) && (this.orderBy_.isInverse(this.dataset_.getOrderByClause())));
	}

	public boolean isAscOrdered()
	{
		return ((this.orderBy_ != null) && (this.orderBy_.equals(this.dataset_.getOrderByClause())));
	}

	public void setOrderBy(String paramString)
	{
		this.orderByExpr_ = paramString;
	}

	public void setOmitThTag(String paramString)
	{
		this.omitThTagExpr_ = paramString;
	}

	public void setHtmlClass(String paramString)
	{
		this.htmlClassExpr_ = paramString;
	}

	public void setStyle(String paramString)
	{
		this.htmlStyleExpr_ = paramString;
	}

	public void setAscImg(String paramString)
	{
		this.ascImgExpr_ = paramString;
	}

	public void setDescImg(String paramString)
	{
		this.descImgExpr_ = paramString;
	}

	public void setAlign(String paramString)
	{
		this.alignExpr_ = paramString;
	}

	public void setValign(String paramString)
	{
		this.valignExpr_ = paramString;
	}

	public void setColspan(String paramString)
	{
		this.colSpanExpr_ = paramString;
	}

	public void setRowspan(String paramString)
	{
		this.rowSpanExpr_ = paramString;
	}

	public void setHeight(String paramString)
	{
		this.heightExpr_ = paramString;
	}

	public void setWidth(String paramString)
	{
		this.widthExpr_ = paramString;
	}

	public String getValue()
	{
		return this.value_;
	}

	public void setValue(String paramString)
	{
		this.valueExpr_ = paramString;
	}

}
